package com.cuckoo.mall.thus.order.orderdetail;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bgn.baseframe.base.MVPBaseActivity;
import com.bgn.baseframe.utils.BaseStringUtil;
import com.bgn.baseframe.utils.DateUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.cuckoo.mall.R;
import com.cuckoo.mall.javabean.Order;
import com.cuckoo.mall.thus.order.adapter.OrderDetailAdapter;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * MVPPlugin
 * 邮箱 784787081@qq.com
 */
@Route(path = "/mall/orderdetailActivity", group = "mall")
public class OrderdetailActivity extends MVPBaseActivity<OrderdetailContract.View, OrderdetailPresenter> implements OrderdetailContract.View {

    @BindView(R.id.tv_receiver_name)
    TextView tvReceiverName;
    @BindView(R.id.tv_receiver_phone)
    TextView tvReceiverPhone;
    @BindView(R.id.tv_receiver_address)
    TextView tvReceiverAddress;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_state)
    TextView tvState;

    @BindView(R.id.tv_order_num)
    TextView tvOrderNum;

    /*shuju*/
    private String order_number;
    private Order order;
    /*Recyclevview*/
    @BindView(R.id.recycle_view)
    RecyclerView recycleView;
    private OrderDetailAdapter orderDetailAdapter;

    /*底部三个价*/
    @BindView(R.id.tv_total_price)
    TextView tv_total_price;
    @BindView(R.id.tv_order_price)
    TextView tv_order_price;
    @BindView(R.id.tv_ticket)
    TextView tvTicket;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_detail);
        ButterKnife.bind(this);
        getTitleBar().setTitle(getString(R.string.order_details));
        order_number = getIntent().getStringExtra("order_number");
        if (TextUtils.isEmpty(order_number)) {
            finish();
        } else {
            initData();
        }

    }

    private void initData() {
        if (isNetworkerConnectHint()) {
            showLoadingView();
            mPresenter.getOrderDetailById(order_number);
        } else {
            showDefaultView();
        }
    }

    @Override
    public void onDefaultViewClick(int tag) {
        if (isNetworkerConnectHint()) {
            hideDefaultView();
        }
    }

    @Override
    public void whenGetOrderSucceed(Order data) {
        order = data;
        setOrderData();
    }

    @Override
    public void whenLoadFaild(String msg) {
        showErrorDefaultView(msg);
    }

    private void setOrderData() {
        switch (order.getStatus()) {
            case "1":
                tvState.setText("有效");
            case "21":
                tvState.setText("删除/无效");
            case "3":
                tvState.setText("发货中");
            default:
                tvState.setText("结束");
                break;
        }
        tvReceiverName.setText(order.getName());
        tvReceiverPhone.setText(order.getPhone());
        tvReceiverAddress.setText(order.getProvince() + "  " + order.getCity() + "  " + order.getAddress());
        /*****/
        tvTime.setText(order.getCreated_at());

        if(order.getDiscount_money()!=0){
            tvTicket.setText("-"+order.getCurrency() + " " + BaseStringUtil.getPrioceFormatString(order.getDiscount_money()));
        }

        //tvState.setText(order.getStatus());
        tvOrderNum.setText(order.getOrder_number());
        tv_total_price.setText(order.getCurrency() + " " + BaseStringUtil.getPrioceFormatString(order.getTotal_money()));
        tv_order_price.setText(order.getCurrency() + " " + BaseStringUtil.getPrioceFormatString(order.getPay_money()));
        /****RecycleView*/
        orderDetailAdapter = new OrderDetailAdapter(order.getProduct_list());
        recycleView.setAdapter(orderDetailAdapter);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setSmoothScrollbarEnabled(true);
        linearLayoutManager.setAutoMeasureEnabled(true);
        recycleView.setLayoutManager(linearLayoutManager);
        recycleView.setHasFixedSize(true);
        recycleView.setNestedScrollingEnabled(false);
        orderDetailAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                ARouter.getInstance().build("/mall/goodMultipleDetailActivity").withString(ID, order.getProduct_list().get(position).getProduct_id()).navigation();
            }
        });
    }
}
